package com.kukudb.asaproject.serveice;


import com.kukudb.asaproject.pojo.ToEmail;
import com.kukudb.asaproject.serveice.interfaceTemp.CodeServericeInterface;
import com.kukudb.asaproject.utils.RandomUtils;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class CodeServericeInterfaceImpl implements CodeServericeInterface {
    @Autowired
    EmailServericeImpl emailServerice;
    Logger logger = LoggerFactory.getLogger(CodeServericeInterfaceImpl.class);
    @Override
    public void sendCode(ToEmail email, HttpSession session) {
        //判断session中是否有验证码
        String code = (String) session.getAttribute("code");
        //设置验证码会话过期时间为10分钟
        session.setMaxInactiveInterval(300);
        //如果有验证码，则发送验证码到邮箱
        if (code == null) {
            //生成随机验证码
            code = RandomUtils.generateRandomCode(6);
            //将验证码存入session
            session.setAttribute("code", code);
        }
        //创建email对象

        email.setSubject("验证码");
        email.setContent("您的验证码为：" + code + "，请不要将验证码泄露给他人。");
        logger.info("开始发送验证码到邮箱,验证码为：{}" ,code);
        logger.info("发送验证码到邮箱：{}" ,email.getTos()[0]);
        //发送验证码到邮箱
        emailServerice.commonEmail(email);
        logger.info("sendCode执行完毕");
    }
}
